package cn.zlj.pethome.org.controller;

import cn.zlj.pethome.basic.QueryResult;
import cn.zlj.pethome.basic.query.BaseQuery;
import cn.zlj.pethome.basic.util.AjaxResult;
import cn.zlj.pethome.basic.util.ExcelUtil;
import cn.zlj.pethome.org.domain.ShopAuditLog;
import cn.zlj.pethome.org.query.QueryPage;
import cn.zlj.pethome.org.service.IShopService;
import cn.zlj.pethome.org.domain.Shop;
import cn.zlj.pethome.org.query.ShopQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
            shopService.delete(id);
            return AjaxResult.me().success();
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Shop> list() {

        return shopService.findAll();
    }

    @PostMapping("/list")
    public QueryResult<Shop> shopQueryResult(@RequestBody BaseQuery query){
        return shopService.queryByPage(query);
    }


    @PostMapping("/settlement")
    public AjaxResult regiest(@RequestBody Shop shop) {
            shopService.settlement(shop);
            return AjaxResult.me().success();
    }
    @PostMapping("/settlement/again")
    public AjaxResult again(@RequestBody Shop para) {
        para.setState(0);
        shopService.update(para);
        return AjaxResult.me().success();
    }
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) throws MessagingException {
        shopService.auditPass(shopAuditLog);
            return AjaxResult.me().success();
    }
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog)  {
        shopService.auditReject(shopAuditLog);
            return AjaxResult.me().success();
    }
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
       shopService.active(id);
        return AjaxResult.me().success();
    }
    @GetMapping("/export/excel")
    public AjaxResult exportExcel(HttpServletResponse response) {
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,"","",Shop.class,"店铺列表.xlsx",response);
        return AjaxResult.me().success();
    }
    @PostMapping("/import/excel")
    public AjaxResult importExcel(@PathVariable("file") MultipartFile file) {
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        shopService.batchAdd(shops);
//        for (Shop shop : shops) {
//            shopService.add(shop);
//        }
        return AjaxResult.me().success();
    }

    @GetMapping("/echarts")
    public Map<String,Object> echarts() {
      return   shopService.echarts();
    }

}
